<?php
App::uses('AppModel', 'Model');
/**
 * Site Model
 *
 * @property societe $societe
 * @property reponses_donnee $reponses_donnee
 */
class Site extends AppModel {

	
	
	
	public function reponses_combo($question) {
		return $this->query("
				SELECT id_reponse, libelle_reponse_combo, rang_reponse FROM reponses WHERE id_question =
			".$question['id_question']);
		
	}
	
	
	/*
	 * Renvoie un tableau avec les questions 
	 */
	public function recup_reponses_listes() {
		// Je récupère les questions de type LISTE et les réponses associées
		$result = $this->query("
					SELECT id_question, type_reponse, libelle_question FROM questions q 
						WHERE q.type_reponse = 'LISTE'
			");
		$out = Array();
		foreach($result as $r) {
			$out[$r['q']['id_question']] = Array();
			$out[$r['q']['id_question']]['result'] = $this->reponses_combo($r['q']);
		}
		return $out;
	}
	
		/*
		 * Récupère les réponses possibles en fonction d'une question (pour les listes)
		 */
		public function reponses_a_une_question($question) {
			$result = $this->query(
						"SELECT * FROM reponses r ".
						"WHERE r.id_question = ".$question['id_question']." "
					);
			return $result;
		}
		
		/* Récupère les articles correspondant à l'id de la catégorie d'articles correspondant à la question passée en paramètre 
		 * Paramètre : $question : id de la question
		 */
		public function recup_article_q($question) {
			return $this->query("SELECT * FROM articles WHERE id_categorie = ".$question['id_categorie']);
		}
		
		/* Récupérer tous les articles */
		public function recuperer_articles() {
			$result = $this->query("SELECT * FROM questions q 
										WHERE q.id_categorie <> 'NULL'
				");
			$out = Array();
			foreach($result as $r) {
				$out[$r['q']['id_question']] = Array();
				$out[$r['q']['id_question']]['result'] = $this->recup_article_q($r['q']);
			}
			
			/*
			$out = Array();
			foreach($result as $r) {
				if(!isset($out[$r['q']['id_question']])) {  
					$out[$r['q']['id_question']] = $r['id_question'];
				}
				
			}
			*/
			return $out;
		}
	
	// récupère les questions à afficher pour un site et un questionnaire
	public function questions_a_afficher() {
		// récupère les questions actives par défaut, qui n'ont pas été désactivées par des réponses à d'autres questions
		// les questions à afficher lorsqu'on arrive sur le formulaire
		$result =  $this->query("SELECT * ".
								"FROM questions Question ".
								"JOIN bloc_questions BlocQuestion ON BlocQuestion.id_bloc = Question.id_bloc ".
								"JOIN vox_blocs VoxBloc ON VoxBloc.id_vox_bloc = BlocQuestion.id_vox_bloc ".
								"LEFT JOIN reponses ON reponses.id_question = Question.id_question ".
								"LEFT JOIN action_questions AQ ON AQ.id_reponse = reponses.id_reponse ".
								"LEFT JOIN nouveau_statuts NS ON NS.id_reponse = reponses.id_reponse ".
								"LEFT JOIN statuts_reponses SR ON NS.id_statut_reponse = SR.id_statut_reponse ".
								"LEFT JOIN statuts_reponses SR_defaut ON SR_defaut.id_statut_reponse = reponses.id_statut_reponse ".
								"WHERE VoxBloc.active AND BlocQuestion.active AND Question.active ".
								"ORDER BY VoxBloc.rang_vox_bloc, BlocQuestion.rang_bloc, Question.rang_question, reponses.rang_reponse");
		
		$out = Array();
		foreach($result as $r) {
			// on crée l'array du VoxBloc si il n'existe pas déjà
			if(!isset($out[$r['VoxBloc']['id_vox_bloc']])) {
				$out[$r['VoxBloc']['id_vox_bloc']] = Array();
				$out[$r['VoxBloc']['id_vox_bloc']]['info_VoxBloc'] = $r['VoxBloc'];
				$out[$r['VoxBloc']['id_vox_bloc']]['BlocsQuestions'] = Array();
			}
			// on crée l'array du BlocQuestion si il n'existe pas déjà
			if(!isset($out[$r['VoxBloc']['id_vox_bloc']]['BlocsQuestions'][$r['BlocQuestion']['id_bloc']])) {
				$out[$r['VoxBloc']['id_vox_bloc']]['BlocsQuestions'][$r['BlocQuestion']['id_bloc']] = Array();
				$out[$r['VoxBloc']['id_vox_bloc']]['BlocsQuestions'][$r['BlocQuestion']['id_bloc']]['info_BlocQuestion'] = $r['BlocQuestion'];
				$out[$r['VoxBloc']['id_vox_bloc']]['BlocsQuestions'][$r['BlocQuestion']['id_bloc']]['Questions'] = Array();
			}
			// on ajoute la question si elle n'existe pas déjà
			if(!isset($out[$r['VoxBloc']['id_vox_bloc']]['BlocsQuestions'][$r['BlocQuestion']['id_bloc']]['Questions'][$r['Question']['id_question']]))
			{
				$out[$r['VoxBloc']['id_vox_bloc']]['BlocsQuestions'][$r['BlocQuestion']['id_bloc']]['Questions'][$r['Question']['id_question']] = $r['Question'];
				$out[$r['VoxBloc']['id_vox_bloc']]['BlocsQuestions'][$r['BlocQuestion']['id_bloc']]['Questions'][$r['Question']['id_question']]['reponses'] = Array();
			}
			// on ajoute la réponse si elle n'existe pas déjà
			// $out[$r['VoxBloc']['id_vox_bloc']]['BlocsQuestions'][$r['BlocQuestion']['id_bloc']]['Questions'][$r['Question']['id_question']]['reponses'][] = $r['reponses'];
			
			
			// on ajoute la réponse si elle n'existe pas déjà
			if(!isset($out[$r['VoxBloc']['id_vox_bloc']]['BlocsQuestions'][$r['BlocQuestion']['id_bloc']]['Questions'][$r['Question']['id_question']]['reponses'][$r['reponses']['id_reponse']])) {
				if($r['reponses']['id_reponse'] != null) {
					$out[$r['VoxBloc']['id_vox_bloc']]['BlocsQuestions'][$r['BlocQuestion']['id_bloc']]['Questions'][$r['Question']['id_question']]['reponses'][$r['reponses']['id_reponse']] = $r['reponses'];
					$out[$r['VoxBloc']['id_vox_bloc']]['BlocsQuestions'][$r['BlocQuestion']['id_bloc']]['Questions'][$r['Question']['id_question']]['reponses'][$r['reponses']['id_reponse']]['libelle_statut_par_defaut'] = $r['SR_defaut']['libelle_statut_reponse'];
					$out[$r['VoxBloc']['id_vox_bloc']]['BlocsQuestions'][$r['BlocQuestion']['id_bloc']]['Questions'][$r['Question']['id_question']]['reponses'][$r['reponses']['id_reponse']]['actions'] = Array();
					$out[$r['VoxBloc']['id_vox_bloc']]['BlocsQuestions'][$r['BlocQuestion']['id_bloc']]['Questions'][$r['Question']['id_question']]['reponses'][$r['reponses']['id_reponse']]['change_statut'] = Array();
				}
			}
			// on ajoute les actions si elles ne sont pas nulles
			if($r['AQ']['id_question'] != null) {
				$out[$r['VoxBloc']['id_vox_bloc']]['BlocsQuestions'][$r['BlocQuestion']['id_bloc']]['Questions'][$r['Question']['id_question']]['reponses'][$r['reponses']['id_reponse']]['actions'][$r['AQ']['id_question']] = $r['AQ'];
			}
			// on ajoute les nouveau_statuts si elles ne sont pas nulles
			if($r['NS']['id_reponse'] != null) {
				$r['NS']['libelle_statut'] = $r['SR']['libelle_statut_reponse'];
				$r['NS']['priorite'] = $r['SR']['priorite_statut_reponse'];
				$out[$r['VoxBloc']['id_vox_bloc']]['BlocsQuestions'][$r['BlocQuestion']['id_bloc']]['Questions'][$r['Question']['id_question']]['reponses'][$r['reponses']['id_reponse']]['change_statut'][$r['NS']['id_reponse_statut_change_par']] = $r['NS'];
			}
		}
		//var_dump($out);
		return $out;
	}
	
	// recupere la liste des questions ayant plusieurs regles les influancant
	public function questions_plusieurs_regles() {
		$result =  $this->query("");
	}
/**
 * Primary key field
 *
 * @var string
 */
	public $primaryKey = 'id_site';
/**
 * Display field
 *
 * @var string
 */
	public $displayField = 'nom_site';
/**
 * Validation rules
 *
 * @var array
 */
	public $validate = array(
		'id_site' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'nom_site' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				'message' => 'Ce champ doit être renseigné.',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'adresse_site' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				'message' => 'Ce champ doit être renseigné.',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'code_postal_site' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				'message' => 'Ce champ doit être renseigné.',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
			'numeric' => array(
				'rule' => array('numeric'),
				'message' => 'Veuillez saisir un numérique.',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'ville_site' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				'message' => 'Ce champ doit être renseigné.',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'telephone_site' => array(
			
			'notempty' => array(
				'rule' => array('notempty'),
				'message' => 'Ce champ doit être renseigné.',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'fax_site' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				'message' => 'Ce champ doit être renseigné.',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'eligibilite_site' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				'message' => 'Ce champ doit être renseigné.',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'install_voxity_site' => array(
			'boolean' => array(
				'rule' => array('boolean'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
			'notempty' => array(
				'rule' => array('notempty'),
				'message' => 'Ce champ doit être renseigné.',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'id_societe' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				'message' => 'Ce champ doit être renseigné.',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);

	//The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * hasOne associations
 *
 * @var array
 */
	public $hasOne = array(
		'societe' => array(
			'className' => 'societe',
			'foreignKey' => 'id_societe',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);

/**
 * hasMany associations
 *
 * @var array
 */
	public $hasMany = array(
		'reponses_donnee' => array(
			'className' => 'reponses_donnee',
			'foreignKey' => 'id_site',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		)
	);

}
